package com.example.androidmpcharts;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.androidmpcharts.bean.CompanyBean;
import com.example.androidmpcharts.bean.CompositeIndexBean;
import com.example.androidmpcharts.bean.IncomeBean;
import com.example.androidmpcharts.bean.LineChartBean;
import com.example.androidmpcharts.manager.BarChartManager;
import com.example.androidmpcharts.util.LocalJsonResolutionUtil;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

public class BarChartActivity extends AppCompatActivity {

    private BarChart barChart;
    private BarChartManager barChartManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar_chart);
        barChart = findViewById(R.id.barChart);
        barChartManager = new BarChartManager(barChart);
        LineChartBean lineChartBean = LocalJsonResolutionUtil.JsonToObject(this,
                "line_chart.json", LineChartBean.class);
//        List<CompanyBean> companyBeans = lineChartBean.getGRID0().getResult().getCompany();
//        barChartManager.showBarChart(companyBeans, "净资产收益率（%）", getResources().getColor(R.color.blue));

        //处理数据是 记得判断每条柱状图对应的数据集合 长度是否一致
        LinkedHashMap<String, List<Float>> chartDataMap = new LinkedHashMap<>();
        List<String> xValues = new ArrayList<>();
        List<Float> yValue1 = new ArrayList<>();
        List<Float> yValue2 = new ArrayList<>();
        List<Integer> colors = Arrays.asList(
                getResources().getColor(R.color.blue), getResources().getColor(R.color.orange)
        );

        List<CompanyBean> companyBeans = lineChartBean.getGRID0().getResult().getCompany();
        List<CompositeIndexBean> compositeIndexBeans = lineChartBean.getGRID0().getResult().getCompositeIndexGEM();

        for (CompanyBean valueBean : companyBeans) {
            xValues.add(valueBean.getTradeDate());
            yValue1.add(Float.valueOf(valueBean.getRate()));
        }
        for (CompositeIndexBean compositeIndexBean : compositeIndexBeans) {
            yValue2.add(Float.valueOf(compositeIndexBean.getRate()*100));
        }
        chartDataMap.put("净资产收益率（%）", yValue1);
        chartDataMap.put("行业平均值（%）", yValue2);

        barChartManager.showBarChart(xValues, chartDataMap, colors);

        //点击事件
        barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                e.getX();       //X轴坐标 记得转 int
                e.getY();       //当前柱状图Y轴值
                e.getIcon();    //对应 BarEntry(float x, float y, Drawable icon)
                e.getData();    //对应 BarEntry(float x, float y, Object data)
                Toast.makeText(BarChartActivity.this, "y:"+e.getY(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected() {
            }
        });

    }

}